 opt l-h+
 org $9000





* cmc replayer (fast)
plr EQU $100-9		* 64 bajty - tymczasowe dane dla playera
whj equ plr+$3d

* odtwarzanie nowej pozycji w songu

; skok w dol o liczbe linii - |
down lda music+$255,y
 clc
 adc plr+$d
 sta plr+$d
 jmp pchn

; skok w gore o liczbe linii - |
up lda plr+$d
 sec
 sbc music+$255,y
 sta plr+$d
 jmp pchn

; jump do okreslonej linii
jump lda music+$255,y
 cmp plr+$d
jump1 sta plr+$d
 bne pchn

; stop play song - |
stop dec plr+$d
 lda #$80
 sta plr+$44
 sta plr+$45
 sta plr+$46
 rts

* nastepny krok w songu
nexstep ldx #0
 stx plr+$41
 stx plr+$42
 stx plr+$43

 stx plr+$44
 stx plr+$45
 stx plr+$46

* sprawdz czy nie ma petli w songu

 lda plr+$16
 beq pchn
 dec plr+$1a
 bne pchn
 dec plr+$16
 beq pchn
 lda plr+$1b
 sta plr+$d
 lda plr+$50
 sta plr+$1a

* sprawdz kolejne pozycje w songu

pchn ldy plr+$d
pchn1 ldx music+$200,y
 bpl pin

 cpx #$8f
 beq stop
 cpx #$bf
 beq down
 cpx #$af
 beq up
 cpx #$fe
 beq free
 cpx #$cf
 beq cht
 cpx #$df
 beq replay
 cpx #$9f
 beq jump
 cpx #$ef
 bne pin

* koniec songu (od nowa) - |

break ldy whj
 jmp jump1

* pobierz pozycje songu i wpisz aktualny adres patternu

pin equ *

 lda music+$14,x	* lsb adresu patternu ch 0
 sta plr+$3e

 lda music+$54,x 	* msb adresu patternu
 sta plr+$4a

 ldx music+$255,y

 lda music+$14,x	* lsb adresu patternu ch 1
 sta plr+$3f

 lda music+$54,x 	* msb adresu patternu
 sta plr+$4b

 ldx music+$2aa,y

 lda music+$14,x	* lsb adresu patternu ch2
 sta plr+$40

 lda music+$54,x 	* msb adresu patternu
 sta plr+$4c
 rts

* pusta pozycja w songu

free ldx #0
free1 iny
 cpy #$54
 bcs ps1
 lda music+$200,y
 cmp #$fe
 beq free1
 sty plr+$0d
 jmp pchn

ps1 stx plr+$0d
 jmp pchn

* zmiana tempa

cht lda music+$255,y
 sta plrtmpo
 sta plr+$52
 inc plr+$d
 jmp pchn

* replay song - ?

replay lda music+$255,y
 sta plr+$50
 sta plr+$1a

 lda music+$2aa,y
 sta plr+$16
 inc plr+$d
 lda plr+$d
 sta plr+$1b
 jmp pchn

* graj nowy instrument

plins sty tu1+1
 sta $fe
 asl @
 tay
 asl @
 asl @

* x8 - adres obwiedni dzwieku

 adc <music+$130
 sta plr+$e,x	* adres obwiedni dzwieku
 lda #0
 sta plr+$31,x
 sta plr+$28,x

 adc >music+$130
 sta plr+$4d,x	* adres obwiedni dzwieku

* x6

 tya
 adc $fe
 asl @
 tay

ct1 lda music+$99,y * accent II byte
 sta plr+$3a,x

ct2 lda music+$98,y * accent I byte
 sta plr+$37,x

ct3 lda music+$97,y  * dlugosc wybrzmiewania dzwieku (co ile ramek -1)
 sta plr+$34,x
 sta plr+$2b,x

ct4 lda music+$96,y  * AB - A o ile polnut w gore , B - dla wibrujacych i 2 kanaowych odstep nut.
 sta $ff

ct5 lda music+$94,y  * AB - B -rodzaj akcentu ($8,$a,$2,$4) , A- wysokosc akcentu
 sta $fe			 * 	(0-c ilosc polnot),(d-f - f max wysoki , e - nizszy itp.)

ct6 lda music+$95,y  * AB - A - znieksztlacenie dzwieku, B - rodzaj dzwieku w CMC.
 sta plr+$2e,x
 and #7
 tay

* wybierz tablice czestotliwosci (na podstawie trybu dzwieku)

 lda tbp1,y
 sta $fc
 lda tbp2,y
 sta $fd

* dodaj do dzwieku ile nut w gore (offset)

 lda $ff
 lsr @
 lsr @
 lsr @
 lsr @
 clc
tu1 adc #0
 sta tu+1

 cpy #7		* 7 dla basu 2 kanalowego (osobna tablica)
 bne pi3

 asl @		* pobierz wartosci z tablicy
 tay
 lda a3,y
 sta plr+$1c,x
 lda a3+1,y
 bcc pi5
 bcs pi5
 
* dla 1 kanalowego lub 2 kanalowego (ale nie basu)

pi3 sta tu4+1
 tay
 lda ($fc),y
 sta plr+$1c,x

 lda $ff
 and #$f
 clc
tu4 adc #0
 tay

 lda ($fc),y
 cmp plr+$1c,x
 bne pi5
 sec
 sbc #1
pi5 sta plr+$1f,x

* rodzaj akcentu - obsluga 

pi4 lda $fe 	* znieksztalcenie z tablicy (dolny nibble) 
 and #3
 tay
 lda k1,y	
 sta plr+$25,x
 
 lda $fe		* przesuniecie w gore
 lsr @
 lsr @
 lsr @
 lsr @
 cmp #$d		* 0 - c normalne
 bcc tu
 tay			* d,e,f - z tablicy
 lda tbp1+1,y
 sta plr+$22,x
 rts
 
tu adc #$ab
 tay
 lda a1,y
 sta plr+$22,x
 rts
 
* poczatek odtwarzania 
* obsluga nmi

play dec plr+$52 	* sprawdz czy nowe tempo
 bne ply4
 lda #0
plrtmpo equ *-1
 sta plr+$52
 
 ldx plr+$c	* nowa pozycja w songu ?
 bne ply3	* tak -
  
 jsr nexstep * aktualizuj song
 ldx plr+$c
 
* odswierzanie pozycji w songu

ply3 inx
 txa
 and #$3f
 sta plr+$c
 bne li7
 inc plr+$0d
 
*  obsluga patternow - odtwarzanie kolejnej pozycji
 
li7 ldx #2

li0 lda plr+$44,x
 beq li1
 dec plr+$44,x
 dex
 bpl li0
 bmi ply4
 
li1 lda plr+$3e,x * pobierz adres pozycji patternu
 sta $fe
 lda plr+$4a,x
 sta $ff

li4 ldy plr+$41,x * odczytaj kolejny bajt
 lda ($fe),y
 bmi li6		
 cmp #$3f
 bcs li3
 sta plr+$47,x	  * zmien instrument
 iny
 tya
 sta plr+$41,x
 lda ($fe),y	  * odczytaj nute

li3 and #$3f
 tay			  * graj nowa nute (y-numer nuty)
 lda plr+$47,x	  * a - nr instrumentu
 jsr plins
 inc plr+$41,x
 dex
 bpl li0
 bmi ply4

li6 and #$3f	* ilosc pustych linii
 sta plr+$44,x
 inc plr+$41,x
li2 dex
 bpl li0
  
* obsluga instrumentow - co 1 ramke

ply4 ldx #2

 lda plr+$2e		* ustawianie znieksztalcenia
 and #$e0
 sta plr+$13

 lda plr+$2f
 and #$e0
 sta plr+$14

 lda plr+$30
 and #$e0
 sta plr+$15

 lda #7
 sta $ff

* obsluga glosnosci

ply lda plr+$28,x
 cmp #$f
 bne xp2
 
* wyciszanie instrumentu
 

 lda plr+$34,x
 beq xp1
 ldy plr+$9,x
 beq xp1
 
 dec plr+$2b,x
 bne xp1
 dec plr+$9,x
 sta plr+$2b,x
 jmp xp1

xp2 lsr @
 tay
 lda plr+$e,x
 sta $fc
 lda plr+$4d,x
 sta $fd
 lda ($fc),y
 bcc xp4
 lsr @
 lsr @
 lsr @
 lsr @
xp4 and #$f
 sta plr+$9,x

 inc plr+$28,x

* obsluga znacznikow

 asl plr+$3a,x
 rol plr+$37,x
 bcc xp1

 lda plr+$22,x
 sta plr+$17,x
 txa
 bne zp3
 sta $ff
zp3 lda plr+$25,x
 sta plr+$13,x

 lda plr+$2e,x
 sta $fe
 beq dp4
 bne dp2
 
xp10 bpl ply

* obsluga czestotliwosci

xp1 ldy plr+$1c,x
 lda plr+$2e,x
 sta $fe
 and #7
 cmp #1
 bne yp1
 dey
 tya
 iny
 cmp plr+$1f,x
 beq yp3
 lda #1		* vibrato I
 bne yp2
yp3 lda #4	* vibrato II
yp2 and plr+$31,x
 beq yp1
 ldy plr+$1f,x
 cpy #$ff
 bne yp1
 lda #0
 sta plr+$9,x
yp1 tya
 sta plr+$17,x

* slide down

dp1 lda $fe
dp2 and #8
 beq dp3
 dec plr+$1c,x
 lda plr+$1f,x
 cmp #$ff
 beq dp5
 dec plr+$1f,x
dp5 dex
 bpl xp10
 bmi ply1

* slide up

dp3 lda $fe
 and #16
 beq dp4
 inc plr+$1c,x
 lda plr+$1f,x
 cmp #$ff
 beq dp4
 inc plr+$1f,x
dp4 dex
 bpl xp10

ply1 inc plr+$31
 inc plr+$32
 inc plr+$33

 lda plr+$18
 sta $d210
 sta $d200
 lda plr+$a
 ora plr+$14
 sta $d211
 sta $d201
 inx

 ldy plr+$1f
 lda $fe
 and $ff
 cmp #5
 beq znacznik1
 cmp #6
 bne znacznik2
znacznik1 ldx #2
 dey
znacznik2 sty $d216
 sty $d206
 cmp #7
 beq b2ass
 tay
 stx $d218
 stx $d208

 ldx plr+$19

 lda plr+$b
 ora plr+$15
 sta $d215
 sta $d205
 
 lda plr+$9
 ora plr+$13
 sta $d213
 sta $d203
 and k4,y
 ldy plr+$17
 
 jmp endplay

b2ass lda #$28
 sta $d218
 sta $d208

 stx $d215
 stx $d205

 ldx plr+$17
 
 lda plr+$b
 ora plr+$15
 sta $d213
 sta $d203

 lda plr+$9
 ora plr+$13
 ldy plr+$19
 
endplay stx $d214
 stx $d204
 sty $d212
 sty $d202
 sta $d217
 sta $d207
    
 rts


minit EQU *
 lda music+$13
 sta plrtmpo

 lda #1
 sta plr+$52

 ldy #8
lpi1 lda #0
 sta $d210,y
 sta $d200,y
 cpy #3
 bcs lpi2
 lda #$f
 sta plr+$28,y
lpi2 dey
 bpl lpi1

 lda #0
 sta plr+$16
 ldx #3
 stx $d21f
 stx $d20f
 dex
ip6 sta plr+$41,x
 sta plr+$44,x
 sta plr+$9,x
 dex
 bpl ip6
 sta plr+$c
 sta plr+$d
; sta whj
 rts












k1 dta b($80,$a0,$20,$40)
k4 dta b(0,0,0,0,$ff,0,$ff)
tbp1 dta l(a1,a1,a1,a2,a1,a1,a1)
tbp2 dta h(a1,a1,a1,a2,a1,a1,a1),d'/(!'

a1 dta b($ff,$f1,$e4,$d7,$cb,$c0,$b5,$aa,$a1,$98,$8f,$87,$7f,$78,$72,$6b)
   dta b($65,$5f,$5a,$55,$50,$4b,$47,$43,$3f,$3c,$38,$35,$32,$2f,$2c,$2a)
   dta b($27,$25,$23,$21,$1f,$1d,$1c,$1a,$18,$17,$16,$14,$13,$12,$11,$10)
   dta b($0f,$0e,$0d,$0c,$0b,$0a,$09,$08,$07,$06,$05,$04,$03,$02,$01)
*   dta b($00,$00)
   
* oryginalny bass
a2 equ *
*   dta b($00,$00,$00,$00,$f2,$e9,$da,$ce,$bf,$b6,$aa,$a1,$98,$8f,$89,$80)
*   dta b($7a,$71,$6b,$65,$5f,$00,$56,$50,$67,$60,$5a,$55,$51,$4c,$48,$43)
*   dta b($3f,$3d,$39,$34,$33,$39,$2d,$2a,$28,$25,$24,$21,$1f,$1e,$00,$00)
*   dta b($0f,$0e,$0d,$0c,$0b,$0a,$09,$08,$07,$06,$05,$04,$03,$02,$01,$00)
*   dta b($00)

* bass alternative table 

  dta b($00,$00,$00,$00,$f2,$e9,$da,$ce,$bf,$b6,$aa,$a1,$98,$8f,$89,$80)
  dta b($7a,$71,$6b,$65,$5f,$5c,$56,$50,$4d,$47,$44,$41,$3e,$38,$35,$88)
  dta b($7f,$79,$73,$6c,$67,$60,$5a,$55,$51,$4c,$48,$43,$3f,$3d,$39,$34)
  dta b($33,$30,$2d,$2a,$28,$25,$24,$21,$1f,$1e,$05,$04,$03,$02,$01,$00)
  dta b($00)

a3 dta b($38,$0b,$8c,$0a,$00,$0a,$6a,$09,$e8,$08,$6a,$08,$ef,$07,$80,$07)
   dta b($08,$07,$ae,$06,$46,$06,$e6,$05,$95,$05,$41,$05,$f6,$04,$b0,$04)
   dta b($6e,$04,$30,$04,$f6,$03,$bb,$03,$84,$03,$52,$03,$22,$03,$f4,$02)
   dta b($c8,$02,$a0,$02,$7a,$02,$55,$02,$34,$02,$14,$02,$f5,$01,$d8,$01)
   dta b($bd,$01,$a4,$01,$8d,$01,$77,$01,$60,$01,$4e,$01,$38,$01,$27,$01)
   dta b($15,$01,$06,$01,$f7,$00,$e8,$00,$db,$00,$cf,$00,$c3,$00,$b8,$00)
   dta b($ac,$00,$a2,$00,$9a,$00,$90,$00,$88,$00,$7f,$00,$78,$00,$70,$00)
   dta b($6a,$00,$64,$00,$5e,$00,$57,$00,$52,$00,$32,$00,$0a,$00)


run jsr minit

wt lda $d40b
 bne wt
 jsr play
 jmp wt






 run run







